<?php

namespace App\Http\Controllers;


use App\Models\Area;
use App\Models\Cts;
use App\Models\Flow;
use App\Models\Scene;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Input;

class MobileController extends AuthController
{
    public function index(){

    }
    public function scenes(){
        return response()->json(Scene::where('is_docking',1)->get(['id','title','latlng']));
    }
    public function flow(){
        $scene_id = Input::get('scene_id');
        $date = Input::has('date')?Input::get('date'):Carbon::today()->toDateString();
        $where = $scene_id!=0?[['scene_id',$scene_id]]:[];
            $query = Flow::select(['in','out','time'])->where($where)->where('time','<>',0)->where('date',$date);//Carbon::today()->toDateString()
            $in = $query->get()->groupBy('time')->sortBy(function ($val,$key){return $key;})->map(function ($val){
                return $val->sum('in');
            });
            $out = $query->get()->groupBy('time')->map(function ($val){
                return $val->sum('out');
            });
            $data = [
                'x'=>config('options.flow_hours'),
                'in'=>$in->values(),
                'out'=>$out->values()
            ];
        return response()->json($data);
    }

    public function sceneAnalysis(){
        $x = Area::pluck('title','id');
        $five = $x->map(function ($val,$key){
            $count = Scene::where('area_id',$key)->where('level',0)->count();
            return $count;
        });
        $four = $x->map(function ($val,$key){
            $count = Scene::where('area_id',$key)->where('level',1)->count();
            return $count;
        });
        $three = $x->map(function ($val,$key){
            $count = Scene::where('area_id',$key)->where('level',2)->count();
            return $count;
        });
        $two = $x->map(function ($val,$key){
            $count = Scene::where('area_id',$key)->where('level',3)->count();
            return $count;
        });
        return response()->json([
            'x'=>$x->values(),
            '5A'=>$five->values(),
            '4A'=>$four->values(),
            '3A'=>$three->values(),
            '2A'=>$two->values(),
        ]);
    }

    public function ctsAnalysis(){
        $x = Area::pluck('title','id');
        $A = $x->map(function ($val,$key){
            $count = Cts::where('area_id',$key)->where('level',0)->count();
            return $count;
        });
        $B = $x->map(function ($val,$key){
            $count = Cts::where('area_id',$key)->where('level',1)->count();
            return $count;
        });
        $C = $x->map(function ($val,$key){
            $count = Cts::where('area_id',$key)->where('level',2)->count();
            return $count;
        });
        $last = $x->map(function ($val,$key){
            $count = Cts::where('area_id',$key)->where('level',3)->count();
            return $count;
        });
        $un = $x->map(function ($val,$key){
            $count = Cts::where('area_id',$key)->where('level',4)->count();
            return $count;
        });
        return response()->json([
            'x'=>$x->values(),
            'A级'=>$A->values(),
            'B级'=>$B->values(),
            'C级'=>$C->values(),
            '末级'=>$last->values(),
            '未定级'=>$un->values(),
        ]);
    }
}
